package ae_game;

import static org.lwjgl.opengl.GL11.*;
import engine.GL_Display;
import engine.gui.Button;
import engine.gui.FrameSelectServer;

import java.awt.Font;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.Vector;


import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.openal.AL;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GLContext;
import org.newdawn.slick.Color;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;


import lang.*;



enum GameState
{
	ServerSelect, LogIn, CharacterSelect;
}

public class aeMain
{

	/**
	 * @param args
	 */
	//WIDTH=800,HEIGHT=600,FPS=60;//
	private SettingsLoader settingsLoader = new SettingsLoader();
	
	private static final int WIDTH=Toolkit.getDefaultToolkit().getScreenSize().width, HEIGHT=Toolkit.getDefaultToolkit().getScreenSize().height, FPS=60;
	private static GL_Display glDisplay;
	private static GameState gState=GameState.ServerSelect;
	
	private static UnicodeFont fontFPS;
	private static int fps=FPS;
	
	private Lang_EN textPack;
	private Vector<Texture> Textures = new Vector<Texture>();
	
	private FrameSelectServer frameSelectServer;
	
	public aeMain()
	{
		//set language to English
		textPack = new Lang_EN();
		
		glDisplay = new GL_Display();
		
		glDisplay.AQstart(WIDTH,HEIGHT,"aeterna [0.0.1]", true);
		glClearColor((float)0.0,(float)0.0, (float)0.0, 0);
		Keyboard.enableRepeatEvents(true);
		
		frameSelectServer = new FrameSelectServer(WIDTH, HEIGHT);
		
		SetFonts();
		LoadTextures();
		
		System.out.println(Lang_EN.getString("OK")+Lang_EN.getString("CANCEL"));
		//textPack = new Lang_RU();
		System.out.println(Lang_EN.getString("OK")+Lang_EN.getString("CANCEL"));
		
		while(!Display.isCloseRequested())
		{
			glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//Clear screen
			int delta = glDisplay.AQgetDelta();
			
			glColor3f(1, 1, 1);//Drop color to default white(correct texture rendering)
			
			switch(gState)
			{
			case ServerSelect:
				renderServerSelect();
				updateServerSelect(delta);
				break;
			}
			//show fps
			int tFPS = glDisplay.AQupdateFPS();
			if(tFPS>0)
				fps=tFPS;
			glLoadIdentity();
			fontFPS.drawString(0, 0, "FPS: "+fps, Color.green);
			//SoundStore.get().poll(0);//Polling audio buffers
			Display.update();
			Display.sync(FPS);
		}
		Exit();
	}
	
	void LoadTextures()
	{
		try
		{
			Textures.add(TextureLoader.getTexture("PNG", getClass().getResourceAsStream("res/img/main_back.png")));
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	void ReleaseTextures()
	{
		for(int i=0; i<Textures.size(); ++i)
			Textures.get(i).release();
	}
	
	void SetFonts()
	{	
	    try
	    {
	    	Font awtFont = new Font("", Font.BOLD, 16);
	    	fontFPS = new UnicodeFont(awtFont);
	    	fontFPS.getEffects().add(new ColorEffect(java.awt.Color.white));
	    	fontFPS.addAsciiGlyphs();
	    	fontFPS.loadGlyphs();
	    } catch (SlickException e)
	    {
	    	e.printStackTrace();
	    } 
	}
	
	void renderServerSelect()
	{
		glColor3f(1, 1, 1);
		Textures.get(0).bind();
		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2i(0, 0);
			glTexCoord2f(1, 0);
			glVertex2i(WIDTH-1, 0);
			glTexCoord2f(1, 1);
			glVertex2i(WIDTH-1, HEIGHT-1);
			glTexCoord2f(0, 1);
			glVertex2i(0, HEIGHT-1);
		glEnd();
		frameSelectServer.render();
		
	}
	void updateServerSelect( int delta )
	{
		frameSelectServer.update(delta);
	}
	
	void Exit()
	{
		settingsLoader.SaveSettings();
		if(AL.isCreated())
			AL.destroy();
		ReleaseTextures();
		Display.destroy();
		System.exit(0);
	}
	
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		new aeMain();
	}
	
	

}
